function OUTPUT = coaster(img)

% Uses a classifier to find the position of the coaster
img = flipdim(img,2); 
detector = vision.CascadeObjectDetector('starbucksDetector3.xml');
%img = imread('coaster.jpg');
bbox = step(detector, img);
detectedImg = insertObjectAnnotation(img, 'rectangle', bbox, 'coaster');
imshow(detectedImg)
w = floor(bbox(1,3)/ 4)
d = floor(bbox(1,4)/ 4)
coaster = imcrop(img,[bbox(1,1)-w,bbox(1,2)-d,bbox(1,3)+w*2,bbox(1,4)+2*d]);
I = coaster;
coaster = rgb2gray(coaster);

BW = edge(coaster,'canny');

%# dilation-erosion
se = strel('disk', 2);

%# fill holes
BW = imfill(BW, 'holes');
figure;
imshow(BW);

STATS = regionprops(BW, 'Area', 'Extrema');
%get largest value
blobs = size(STATS);

big = 0;
pos = 1;
for i = 1 :blobs(1);
    if  STATS(i).Area > big;
        big = STATS(i).Area;
        pos = i;
    end
end

img = flipdim(img,2);
imsize = size(img);
width = imsize(2);
wid = (bbox(1,1)) - w;
hei = (bbox(1,2)) - d;
point1 = [(width - (STATS(pos).Extrema([1],1) + wid)), (STATS(pos).Extrema([1],2))+hei];
point2 = [(width - (STATS(pos).Extrema([3],1) + wid)), (STATS(pos).Extrema([3],2))+hei];
point3 = [(width - (STATS(pos).Extrema([5],1) + wid)), (STATS(pos).Extrema([5],2))+hei];
point4 = [(width - (STATS(pos).Extrema([7],1) + wid)), (STATS(pos).Extrema([7],2))+hei];

figure; imshow(img); hold on;
plot(point1(1),point1(2),'r.');
plot(point2(1),point2(2),'r.');
plot(point3(1),point3(2),'r.');
plot(point4(1),point4(2),'r.');

OUTPUT = [[point1(1),point2(1),point3(1),point4(1)];[point1(2) point2(2) point3(2) point4(2)]];


